import {
	HTTP_REQUEST_URL,
	HEADER,
	FORMHEADER,
	TOKENNAME
} from '@/config/app';
import {
	toLogin,
	checkLogin
} from '../libs/login';
import store from '../store';


/**
 * 发送请求
 */
function baseRequest(url, method, data, {
	noAuth = false,
	noVerify = false,
	formHeader = false
}) {
	let Url = HTTP_REQUEST_URL,
		header = formHeader ? FORMHEADER : HEADER;
	if (!noAuth) {
		//登录过期自动登录
		if (!store.state.app.token && !checkLogin()) {
			toLogin();
			return Promise.reject({
				msg: '未登录'
			});
		}
	}

	if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
	if (data != undefined) {
		data['bm_system'] = uni.getStorageSync('system');
		data['bm_uniPlatform'] = uni.getStorageSync('uniPlatform');
		data['bm_model'] = uni.getStorageSync('model');
		data['bm_browserName'] = uni.getStorageSync('browserName');
		data['bm_screenHeight'] = uni.getStorageSync('screenHeight');
		data['bm_deviceBrand'] = uni.getStorageSync('deviceBrand');
		data['bm_brand'] = uni.getStorageSync('brand');
		data['bm_appVersion'] = uni.getStorageSync('appVersion');
		data['bm_deviceModel'] = uni.getStorageSync('deviceModel');
		data['bm_osAndroidAPILevel'] = uni.getStorageSync('osAndroidAPILevel');
		data['bm_romName'] = uni.getStorageSync('romName');
		data['bm_romVersion'] = uni.getStorageSync('romVersion');
		data['bm_platform'] = uni.getStorageSync('platform');
		data['bm_push_clientid'] = uni.getStorageSync('push_clientid'); //唯一推送
		data['bm_addr'] = uni.getStorageSync('addr'); //地址
		data['token'] = store.state.app.token;

	}

 console.log("header: " + JSON.stringify(header));

	return new Promise((reslove, reject) => {
		uni.request({
			url: Url + url,
			method: method || 'GET',
			header: header,
			data: data || {},
			success: (res) => {
				//		console.log(Url + '/' + url);  
				//	console.log('----打印请求：'+url,res);
				if (!res.data.msg || res.data.msg == '') {
					res.data.msg = res.data.message || ''
				}
				if (noVerify) {
					reslove(res.data, res);
				} else if (res.data.status == 200 || res.data.code == 0 || res.data.msg ==
					'success' || res.data.data || res.data.list || res.data.arr || res.data.length >
					-1) {
					reslove(res.data, res);
					//       else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
					//   toLogin();
					//         reject(res.data);
					// }else if(res.data.status == 410010){
					// 	uni.showModal({
					// 	    title: '提示',
					// 	    content: res.data.msg,
					// 		showCancel: false,
					// 		confirmText:'我知道了'
					// 	}); 
					//       } 
				} else {
					reject(res.data.msg || '系统错误');
				}
			},
			fail: (msg) => {
				let data = {
					mag: '请求失败',
					status: 1 //1没网
				}
				// #ifdef APP-PLUS
				reject(data);
				// #endif
				// #ifndef APP-PLUS
				//	reject(JSON.stringify(msg));
				reject('请求失败');
				// #endif
			}
		})
	});
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});



export default request;